package com.example.rabbitspringboot.controller;

import com.example.rabbitspringboot.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Authther: mrwu
 * @date:2021/5/13-05
 * @Description:com.example.rabbitspringboot.controller
 */

//接受用户下单请求
@RestController
@Slf4j
public class OrderController {

    static final String EXCHANGE="ex.order";
    @Autowired
    AmqpTemplate amqpTemplate;

    @Autowired
    OrderService orderService;

    /**
     * 根据商品名称进行下单 返回订单编号
     * @param goodsname 商品名称
     * @return
     */
    @GetMapping("/order")
    public String getOrder(String goodsname){
        //getorderid

        //下单
        //利用mq 异步生产待支付订单任务
       amqpTemplate.convertAndSend("ex.order",
               goodsname, "order:"+goodsname);
       log.info("发送消息"+goodsname);
        return "ok";
    }
    @GetMapping("/pay")
    public int payOrder(String orderid){
        //更新对应订单状态 将订单状态修改为已支付
        int status = orderService.getById(orderid).getStatus();
        //已过期
        if (status==2){
            //前段页面根据返回值情况跳转页面
            return 1;
        }else {
            //获取下单参数
            orderService.payOrderById(orderid);
            return 0;
        }

    }


}
